package ru.narod.amidoriya.app.sushibaramidoriya;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.LocalActivityManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnDragListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.SlidingDrawer.OnDrawerScrollListener;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;
import com.google.ads.*;

public class MainActivity extends Activity {
	Integer ctbl1;
	Integer contpoz;
	Double sumpoz;
	int nameColIndex;
	final int TASK_CODE = 1;
	public final static int STATUS_START = 100; 
	public final static int STATUS_FINISH = 200;
	public final static int STATUS_QUERY = 300;

	public final static String PARAM_RUNS = "runs"; // 0 ���������������, 1 �������������
	public final static String PARAM_TASK = "task"; 
	public final static String PARAM_PINTENT = "pendingIntent";
	public final static String PARAM_RESULT = "result";
	public final static String PARAM_STATUS = "status";

	ImageView inetorg, ihandle; 
	TextView zakaztel; 
	ImageView infoorg;
	OnClickListener appoclbtn;

	Boolean spchbload;
	Boolean spchberror;
	Boolean chbpach;
	String spprisepath;
	public String spprisepathfull, pricenomediafull;
	public ProgressDialog pd;
	public ProgressDialog pdtask;
	
	int pdshow = 0;
	int cract = 0;
	SharedPreferences sp;
	public static File EDIR = Environment.getExternalStorageDirectory();
	Intent intent;

	PendingIntent pi;
	final String LOG_TAG = "myLogs";

	XmlPullParser parser, parserpxml;
	InputStream xmlfile, xmlfilepxml;
	String tmp, tmp_stag,tmp_etag,tmp_text_tag,tmp_atag;
	LocalActivityManager mlam;
	
	ArrayList<HashMap<String, String>> bludodata1;
	HashMap<String, String> mbv1;
	ArrayList<HashMap<String, String>> bludodata2;
	HashMap<String, String> mbv2;
	ArrayList<HashMap<String, String>> bludodata3;
	HashMap<String, String> mbv3;
	ArrayList<HashMap<String, String>> bludodatatrash;
	HashMap<String, String> mbvtrash;
	ListBludo ItemLBludoAdapter1;
	ListBludo ItemLBludoAdapter2;
	ListBludo ItemLBludoAdapter3;
	ListBludoTrash ItemLBludoAdaptertrash;
	ListView llistbludo1;
	ListView llistbludo2;
	ListView llistbludo3;
	ListView llistbludotrash;
	TextView not_list_zakaz_;
	BludoTask bt;
	BludoTaskTrash bt_trash, bt_trashclearall;
	
	DisplayMetrics dm1;
	int sizes,sizesg;
	Bitmap appdri;
	
	SlidingDrawer slDrawer;
	TextView adress;
	
	Zakaz sqldbc;
	SQLiteDatabase sqldb;
	Button button_clear_;
	TextView textView_itogo_;
	private CharSequence[] spisokdmenulogo;
	AlertDialog.Builder dialogmenulogo;

	TextView textViewfirma;
	ImageView imageaktion;
	AdView adView;
	ImageView mhandle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	      
		setContentView(R.layout.activity_main);
		
		llistbludo1 = (ListView) findViewById(R.id.list_tab1);
		llistbludo2 = (ListView) findViewById(R.id.list_tab2);
		llistbludo3 = (ListView) findViewById(R.id.list_tab3);
		llistbludotrash = (ListView) findViewById(R.id.list_zakazs);
		not_list_zakaz_ = (TextView) findViewById(R.id.not_list_zakaz);
		slDrawer = (SlidingDrawer) findViewById(R.id.slDrawer);
		adress = (TextView) findViewById(R.id.textView_adress);
		spisokdmenulogo = getResources().getTextArray(R.array.menulogomain);
		mhandle = (ImageView) findViewById(R.id.handle);
	
		
		textViewfirma = (TextView) findViewById(R.id.TextView_firma);
		sp = PreferenceManager.getDefaultSharedPreferences(this); 
		
		spchbload = sp.getBoolean("chbload", true);
		spchberror = sp.getBoolean("chberror", false);
		chbpach = sp.getBoolean("chbpach", false);
		if (chbpach == true) {
			spprisepath =  sp.getString("prisepath", EDIR + File.separator + getString(R.string.dir_menu));

		} 
		else {
			spprisepath = EDIR + File.separator + getString(R.string.dir_menu);	

		}


		spprisepathfull = spprisepath+File.separator+getString(R.string.app_menu);
		pricenomediafull = spprisepath+File.separator+getString(R.string.app_nomedia);

		try {

			File flprice = new File(spprisepathfull);
			flprice.getParentFile().mkdirs();
			if	(flprice.getParentFile().isDirectory()) {

			}
			else {
				spprisepathfull = EDIR + File.separator + getString(R.string.dir_menu)+File.separator+getString(R.string.app_menu);	
				pricenomediafull = EDIR + File.separator + getString(R.string.dir_menu)+File.separator+getString(R.string.app_nomedia);
			}

		} catch (Exception e) {
			spprisepathfull = EDIR + File.separator + getString(R.string.dir_menu)+File.separator+getString(R.string.app_menu);	
			pricenomediafull = EDIR + File.separator + getString(R.string.dir_menu)+File.separator+getString(R.string.app_nomedia);
		}
		dm1 = new DisplayMetrics();
		BitmapFactory.Options o = new BitmapFactory.Options();
		o.inTargetDensity = dm1.densityDpi;
		
		appdri = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher,null);
		
		sizes = (int) Math.round(appdri.getWidth()*1.3);
		sizesg = (int) Math.round(appdri.getWidth());
		
		pd = new ProgressDialog(this);
		pdtask = new ProgressDialog(this);
		
		// �������������� ��� �����
		sqldbc = new Zakaz(getApplicationContext());
		
		// ���� ������ � ������
		sqldb = sqldbc.getWritableDatabase();
		
		//� ��������� �����		
		Handler handlerFileOpen = new Handler();
		handlerFileOpen.post(new Runnable() {
			@Override
			public void run() {				
				nomediapfos(pricenomediafull);
				openlist();
			
			}
			
		});
	////\\\\
		
      final TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
      
      				tabHost.setup();
      			
					TabHost.TabSpec tabSpec; 
					tabSpec = tabHost.newTabSpec("tag1");
					
					tabSpec.setIndicator(getString(R.string.stab1)); 
					tabSpec.setContent(R.id.list_tab1);
					
					tabHost.addTab(tabSpec);

					tabSpec = tabHost.newTabSpec("tag2"); 
					
					tabSpec.setIndicator(getString(R.string.stab2)); 
					tabSpec.setContent(R.id.list_tab2);
					
					tabHost.addTab(tabSpec);

					tabSpec = tabHost.newTabSpec("tag3");
					
					tabSpec.setIndicator(getString(R.string.stab3));
					tabSpec.setContent(R.id.list_tab3); 
					
					tabHost.addTab(tabSpec);

					// ������ ������� ����� ������� �� ��������� 
					tabHost.setCurrentTabByTag("tag1"); 
					
					slDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
					
						@Override
						public void onDrawerOpened() {							
							//mhandle.setImageResource(R.drawable.amidoriy_shop_open);
						}
					});
					
					slDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
						
						@Override
						public void onDrawerClosed() {
							//mhandle.setImageResource(R.drawable.amidoriy_shop_close);	
						}
					});
					slDrawer.setOnDrawerScrollListener(new OnDrawerScrollListener() {

						@Override
						public void onScrollEnded() {
							// TODO Auto-generated method stub
							
						}

						@Override
						public void onScrollStarted() {
							if (!slDrawer.isOpened()) {
							trashHandler();	
							}
						}
							
					});
					
						tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
							
							public void onTabChanged(String tabId) { 
									
							}
						});
						
					////\\\\		

		///
		 

		inetorg = (ImageView) findViewById(R.id.imageinet);
		zakaztel = (TextView) findViewById(R.id.textzak); 
		infoorg = (ImageView) findViewById(R.id.imageinfo);
		ihandle = (ImageView) findViewById(R.id.handle);
		button_clear_ = (Button)findViewById(R.id.button_clear);
		textView_itogo_  = (TextView) findViewById(R.id.textView_itogo);
		imageaktion = (ImageView) findViewById(R.id.imageaktion);
		
		inetorg.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				try {	
					String sweb = getString(R.string.web);
					Intent brouzintent = new Intent(Intent.ACTION_VIEW,Uri.parse(sweb));
					startActivity(brouzintent);
				} catch (Exception e) {

				}
			}	
		});
		inetorg.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
					Menulogo();
				return false;
			}
		});
		zakaztel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				try {	
					String stel = getString(R.string.telprzakaz);
					Intent diallintent = new Intent(Intent.ACTION_DIAL,Uri.parse(stel));
					startActivity(diallintent);
				} catch (Exception e) {

				}
			}	
		});
		infoorg.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				try {
					startActivityForResult(new Intent(getApplicationContext(), Aboutorg.class),0);
				} catch (Exception e) {

				}
			}	
		});
		imageaktion.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {

					try {
						startActivityForResult(new Intent(getApplicationContext(), Aktions.class),0);
					} catch (Exception e) {

					}
				}	
			});
			
		
		///AsyncTask
		 bt = new BludoTask(); 
		 bt.execute(spprisepath, spprisepathfull);
	    
	     llistbludo1.setOnItemClickListener (new OnItemClickListener() {
	 

			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				
	    		if (!slDrawer.isOpened()) {
					try {
						Intent intent = new Intent(getApplicationContext(),Bludo.class);
						
   						intent.putExtra("namebludo", bludodata1.get(position).get("b_0").toString());
   						intent.putExtra("pricebludo", bludodata1.get(position).get("b_1").toString());
   						intent.putExtra("dostavkabludo", bludodata1.get(position).get("b_2").toString());
   						intent.putExtra("weightpiudo", bludodata1.get(position).get("b_3").toString());
   						intent.putExtra("picsmallpludo", bludodata1.get(position).get("b_4").toString());
   						intent.putExtra("filepicsmallpludoPath", bludodata1.get(position).get("b_5").toString());
   						intent.putExtra("sostavbludo", bludodata1.get(position).get("b_6").toString());
   						
						intent.putExtra("spprisepath", spprisepath);
						intent.putExtra("spprisepathfull", spprisepathfull);
						startActivityForResult(intent,0);
					} catch (Exception e) {

					}
					}
			}

							
			}  );
			
	     llistbludo1.setOnItemLongClickListener (new OnItemLongClickListener() {
				public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
					if (!slDrawer.isOpened()) {
					try {
						ListBludo ItemLBludoAdapter1_ = new ListBludo(getApplicationContext(), bludodata1, sizes);
						ItemLBludoAdapter1_.viewbludocount(position,view);
												
					} catch (Exception e) {

					}
					}
					return true;
				}
			}  );
	     llistbludo2.setOnItemClickListener (new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					if (!slDrawer.isOpened()) {
					try {
						Intent intent = new Intent(getApplicationContext(),Bludo.class);
						
						intent.putExtra("namebludo", bludodata2.get(position).get("b_0").toString());
   						intent.putExtra("pricebludo", bludodata2.get(position).get("b_1").toString());
   						intent.putExtra("dostavkabludo", bludodata2.get(position).get("b_2").toString());
   						intent.putExtra("weightpiudo", bludodata2.get(position).get("b_3").toString());
   						intent.putExtra("picsmallpludo", bludodata2.get(position).get("b_4").toString());
   						intent.putExtra("filepicsmallpludoPath", bludodata2.get(position).get("b_5").toString());
   						intent.putExtra("sostavbludo", bludodata2.get(position).get("b_6").toString());
   					
						intent.putExtra("spprisepath", spprisepath);
						intent.putExtra("spprisepathfull", spprisepathfull);
						startActivityForResult(intent,0);
					} catch (Exception e) {

					}
					}
				}
			}  );
	     llistbludo2.setOnItemLongClickListener (new OnItemLongClickListener() {
				public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
					if (!slDrawer.isOpened()) {
					try {
						ListBludo ItemLBludoAdapter2_ = new ListBludo(getApplicationContext(), bludodata2, sizes);
						ItemLBludoAdapter2_.viewbludocount(position,view);
												
					} catch (Exception e) {

					}
					}
					return true;
				}
			}  );
	     llistbludo3.setOnItemClickListener (new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					if (!slDrawer.isOpened()) {
					try {
						Intent intent = new Intent(getApplicationContext(),Bludo.class);
						
						intent.putExtra("namebludo", bludodata3.get(position).get("b_0").toString());
   						intent.putExtra("pricebludo", bludodata3.get(position).get("b_1").toString());
   						intent.putExtra("dostavkabludo", bludodata3.get(position).get("b_2").toString());
   						intent.putExtra("weightpiudo", bludodata3.get(position).get("b_3").toString());
   						intent.putExtra("picsmallpludo", bludodata3.get(position).get("b_4").toString());
   						intent.putExtra("filepicsmallpludoPath", bludodata3.get(position).get("b_5").toString());
   						intent.putExtra("sostavbludo", bludodata3.get(position).get("b_6").toString());
   					
						intent.putExtra("spprisepath", spprisepath);
						intent.putExtra("spprisepathfull", spprisepathfull);
						startActivityForResult(intent,0);
					} catch (Exception e) {

					}
					}
				}
			}  );
	     llistbludo3.setOnItemLongClickListener (new OnItemLongClickListener() {
				public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
					if (!slDrawer.isOpened()) {
					try {
						ListBludo ItemLBludoAdapter3_ = new ListBludo(getApplicationContext(), bludodata3, sizes);
						ItemLBludoAdapter3_.viewbludocount(position,view);
												
					} catch (Exception e) {

					}
					}
					return true;
				}
			}  );
			
	 
	 	adress.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {					
					Uri geo = Uri.parse("geo:0,0?q="+getString(R.string.adres));
					Intent geoMap = new Intent(Intent.ACTION_VIEW, geo);
					startActivity(geoMap);					
				} catch (Exception e) {

				}

			}	
		});
	 	
	 	

	 	llistbludotrash.setOnItemClickListener (new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				
				try {
					Intent intenttrash = new Intent(getApplicationContext(),Bludo.class);
					
					intenttrash.putExtra("namebludo", bludodatatrash.get(position).get("tr_0").toString());
					intenttrash.putExtra("pricebludo", bludodatatrash.get(position).get("tr_1").toString());
					intenttrash.putExtra("dostavkabludo", bludodatatrash.get(position).get("tr_2").toString());
					intenttrash.putExtra("weightpiudo", bludodatatrash.get(position).get("tr_3").toString());
					intenttrash.putExtra("picsmallpludo", bludodatatrash.get(position).get("tr_4").toString());
					intenttrash.putExtra("filepicsmallpludoPath", bludodatatrash.get(position).get("tr_5").toString());
					intenttrash.putExtra("sostavbludo", bludodatatrash.get(position).get("tr_6").toString());
					
					intenttrash.putExtra("spprisepath", spprisepath);
					intenttrash.putExtra("spprisepathfull", spprisepathfull);
					startActivityForResult(intenttrash,0);
				} catch (Exception e) {

				}
				
			}
		}  );
	 	button_clear_.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {					
				//�������� �� �������
					String deltrashall = "DELETE FROM " + 
						    Zakaz.dtable + " ";
			     				    	
			     // �������������� ��� �����
			    	Zakaz sqldbctrashall = new Zakaz(getApplicationContext());
					// ���� ������ � ������
					SQLiteDatabase sqldbtrashall = sqldbctrashall.getWritableDatabase();
					sqldbtrashall.execSQL(deltrashall);
					sqldbtrashall.close();
					sqldbctrashall.close();
					trashHandler();
					ItemLBludoAdapter1.notifyDataSetChanged();
					ItemLBludoAdapter2.notifyDataSetChanged();
					ItemLBludoAdapter3.notifyDataSetChanged();
				} catch (Exception e) {

				}

			}	
		});
	 	
	 	
			
		
		Handler handleradView = new Handler();
		handleradView.post(new Runnable() {
			@Override
			public void run() {				
				try {
					LinearLayout reklama = (LinearLayout) findViewById(R.id.reklamagoogle1);
					LayoutInflater reklamaInflater = getLayoutInflater();
					View reklamaorg = reklamaInflater.inflate(R.layout.reklamagoogle, reklama, true);
					adView = (AdView)reklamaorg.findViewById(R.id.adView);
					adView.loadAd(new AdRequest()); 
				} catch (Exception e) {
					
				}
				
			}
			
		});
	}
	class BludoTaskTrash extends AsyncTask<String, Void, Void> { 
        
	       @Override 
	       protected void onPreExecute() {   	   
	    	   super.onPreExecute(); 
	          ///progress ������
	    	   ProgressTask();
	    	   
	         } 
	       @Override 
	       protected Void doInBackground(String... urls) {
	    	   
	    	   		try {
	    	   			groupbludo();	
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
	    	   		
	    	   		
			      return null; 
			  } 
	       @Override 
	       protected void onPostExecute(Void result) { 
	         super.onPostExecute(result);
	        
	        
	        /// ��������� ������� ������ 
	         pdtask.hide();
	         
	         if (ctbl1 > 0) {
					textView_itogo_.setText(getString(R.string.vsego)+" "+Integer.valueOf(contpoz).toString()+" "+getString(R.string.pozsum)+" "+Double.valueOf(sumpoz).toString()+" "+getString(R.string.valut));
					not_list_zakaz_.setVisibility(View.GONE);
					llistbludotrash.setVisibility(View.VISIBLE);
				} else {
						textView_itogo_.setText(R.string.tr_nol_t);
						llistbludotrash.setVisibility(View.GONE);
						not_list_zakaz_.setVisibility(View.VISIBLE);						
						}
	        
	         llistbludotrash.setAdapter(ItemLBludoAdaptertrash);
	         ItemLBludoAdaptertrash.notifyDataSetChanged();
	       }
	       private void groupbludo() throws InterruptedException {
	    	
		     	String selblt = "SELECT " + Zakaz.DNAMEDISH + ", "
		     			 + Zakaz.DPRICE + ", " + Zakaz.DDOSTAVKA + ", " + Zakaz.DVES + ", " + Zakaz.DPIC + ", " + Zakaz.DPICFULL + ", " + 
		     			 Zakaz.DSOSTAV + 
		     			 ", COUNT(*) " + 
		     			 " FROM " + Zakaz.dtable+
		     			 " GROUP BY "+ Zakaz.DNAMEDISH + ", "
		     			 + Zakaz.DPRICE + ", " + Zakaz.DDOSTAVKA + ", " + Zakaz.DVES + ", " + Zakaz.DPIC + ", " + Zakaz.DPICFULL + ", " + 
		     			 Zakaz.DSOSTAV + 
		     			 " ";
		     	
		    	
		     // �������������� ��� �����
		    	Zakaz sqldbctrash = new Zakaz(getApplicationContext());
				// ���� ������ � ������
				SQLiteDatabase sqldbtrash = sqldbctrash.getWritableDatabase();
				
				Cursor  cursorbl0 = sqldbtrash.rawQuery(selblt, null);
				
				
				ctbl1 =  cursorbl0.getCount();
				bludodatatrash =  new ArrayList<HashMap<String, String>>();
				
				
				
				
				contpoz = 0;
				sumpoz = (double) 0;
				if (ctbl1 > 0) {
					
					while (cursorbl0.moveToNext()) {
						
						mbvtrash = new HashMap<String, String>();
						mbvtrash.put("tr_0", cursorbl0.getString(0));
						mbvtrash.put("tr_1", Double.valueOf(cursorbl0.getDouble(1)).toString());
						mbvtrash.put("tr_2", cursorbl0.getString(2));
						mbvtrash.put("tr_3", cursorbl0.getString(3));
						mbvtrash.put("tr_4", cursorbl0.getString(4));
						mbvtrash.put("tr_5", cursorbl0.getString(5));
						mbvtrash.put("tr_6", cursorbl0.getString(6));
						mbvtrash.put("tr_7", Integer.valueOf(cursorbl0.getInt(7)).toString());
						contpoz = contpoz + cursorbl0.getInt(7);
						sumpoz = sumpoz + (cursorbl0.getInt(7)*cursorbl0.getDouble(1));
						bludodatatrash.add(mbvtrash);
						
					}
					
				} 
				cursorbl0.close();
				sqldbtrash.close();
			    sqldbctrash.close(); 
			   
				ItemLBludoAdaptertrash = new ListBludoTrash(getApplicationContext(), bludodatatrash, sizes, getString(R.string.valut), getString(R.string.izm));
				ItemLBludoAdaptertrash.notifyDataSetChanged();
				
		   		
	       }
	       
	       
	}
	
	class BludoTask extends AsyncTask<String, Void, Void> { 
        
	       @Override 
	       protected void onPreExecute() {   	   
	    	   super.onPreExecute(); 
	          ///progress ������
	    	   ProgressTask();
	    	   
	         } 
	       @Override 
	       protected Void doInBackground(String... urls) {
	    	   
	    	   		try {
	    	   			openlisttab1(urls[0],urls[1]);
						openlisttab2(urls[0],urls[1]);
		    	   		openlisttab3(urls[0],urls[1]);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    	   		
	    	   		
			      return null; 
			  } 
	       @Override 
	       protected void onPostExecute(Void result) { 
	         super.onPostExecute(result);
	         llistbludo1.setAdapter(ItemLBludoAdapter1);
	         llistbludo2.setAdapter(ItemLBludoAdapter2);
	         llistbludo3.setAdapter(ItemLBludoAdapter3);
	        /// ��������� ������� ������ 
	         pdtask.hide();
	         
	       }
	       private void openlisttab1(String bpath1,String bpathfull1) throws InterruptedException {
	    	   bludodata1 =  new ArrayList<HashMap<String, String>>();
	   		try {

	   			XmlPullParserFactory parserxmlpull1 = XmlPullParserFactory.newInstance();
	   			parserxmlpull1.setValidating(false);
	   			XmlPullParser parserxml1 = parserxmlpull1.newPullParser();
	   			File xmlfileprices1 = new File(bpathfull1);
	   			InputStream xmlfiles1 = new FileInputStream(xmlfileprices1);
	   			parserxml1.setInput(xmlfiles1, null);
	   		
	   			while (parserxml1.getEventType()!= XmlPullParser.END_DOCUMENT) {

	   				if (parserxml1.getEventType() == XmlPullParser.START_TAG
	   						&& parserxml1.getName().equals("dish_japan") 
	   						&& parserxml1.getDepth() == 3) {
	   					//
	   					mbv1 = new HashMap<String, String>();
	   					String namebludo = parserxml1.getAttributeValue(0);
	   					if (namebludo == null) {
	   						namebludo = "";
	   					}else {
	   						namebludo = parserxml1.getAttributeValue(0);	
	   					}
	   					//
	   					String pricebludo = parserxml1.getAttributeValue(1);
	   					if (pricebludo == null) {
	   						pricebludo = "";
	   					}else {
	   						pricebludo = parserxml1.getAttributeValue(1);	
	   					}
	   					//
	   					String dostavkabludo = parserxml1.getAttributeValue(2);
	   					if (dostavkabludo == null) {
	   						dostavkabludo = "";
	   					}else {
	   						dostavkabludo = parserxml1.getAttributeValue(2);	
	   					}
	   					//
	   					String weightpiudo = parserxml1.getAttributeValue(3);
	   					if (weightpiudo == null) {
	   						weightpiudo = "";
	   					}else {
	   						weightpiudo = parserxml1.getAttributeValue(3);	
	   					}
	   					String picsmallpludo = parserxml1.getAttributeValue(4);
	   					if (picsmallpludo == null) {
	   						picsmallpludo = "";
	   					}else {
	   						picsmallpludo = parserxml1.getAttributeValue(4);	
	   					}
	   					
	   						String filepicsmallpludoPath = "";						
	   						filepicsmallpludoPath = bpath1+File.separator + picsmallpludo;
	   						
	   						mbv1.put("b_0", namebludo);
	   						mbv1.put("b_1", pricebludo);
	   						mbv1.put("b_2", dostavkabludo);
	   						mbv1.put("b_3", weightpiudo);
	   						mbv1.put("b_4", picsmallpludo);
	   						mbv1.put("b_5", filepicsmallpludoPath);
	   						mbv1.put("b_6", parserxml1.nextText());
	   						bludodata1.add(mbv1);
	   					
	   				}


	   				parserxml1.next();

	   			}


	   			xmlfiles1.close();
	   			
	   			ItemLBludoAdapter1 = new ListBludo(getApplicationContext(), bludodata1, sizes);
	   			ItemLBludoAdapter1.notifyDataSetChanged();
	   				
	   			
	   		}
	   		catch (Throwable t) {
	   				
	   		}	
	   	}
	   	///\\\
	   	public void openlisttab2(String bpath2,String bpathfull2) throws InterruptedException {
	   		bludodata2 =  new ArrayList<HashMap<String, String>>();
	   		try {

	   			XmlPullParserFactory parserxmlpull2 = XmlPullParserFactory.newInstance();
	   			parserxmlpull2.setValidating(false);
	   			XmlPullParser parserxml2 = parserxmlpull2.newPullParser();
	   			File xmlfileprices2 = new File(bpathfull2);
	   			InputStream xmlfiles2 = new FileInputStream(xmlfileprices2);
	   			parserxml2.setInput(xmlfiles2, null);
	   		
	   			while (parserxml2.getEventType()!= XmlPullParser.END_DOCUMENT) {

	   				if (parserxml2.getEventType() == XmlPullParser.START_TAG
	   						&& parserxml2.getName().equals("dish_ukrain") 
	   						&& parserxml2.getDepth() == 3) {
	   					//
	   					mbv2 = new HashMap<String, String>();
	   					String namebludo = parserxml2.getAttributeValue(0);
	   					if (namebludo == null) {
	   						namebludo = "";
	   					}else {
	   						namebludo = parserxml2.getAttributeValue(0);	
	   					}
	   					//
	   					String pricebludo = parserxml2.getAttributeValue(1);
	   					if (pricebludo == null) {
	   						pricebludo = "";
	   					}else {
	   						pricebludo = parserxml2.getAttributeValue(1);	
	   					}
	   					//
	   					String dostavkabludo = parserxml2.getAttributeValue(2);
	   					if (dostavkabludo == null) {
	   						dostavkabludo = "";
	   					}else {
	   						dostavkabludo = parserxml2.getAttributeValue(2);	
	   					}
	   					//
	   					String weightpiudo = parserxml2.getAttributeValue(3);
	   					if (weightpiudo == null) {
	   						weightpiudo = "";
	   					}else {
	   						weightpiudo = parserxml2.getAttributeValue(3);	
	   					}
	   					String picsmallpludo = parserxml2.getAttributeValue(4);
	   					if (picsmallpludo == null) {
	   						picsmallpludo = "";
	   					}else {
	   						picsmallpludo = parserxml2.getAttributeValue(4);	
	   					}
	   					
	   						String filepicsmallpludoPath = "";						
	   						filepicsmallpludoPath = bpath2+File.separator + picsmallpludo;
	   						
	   						mbv2.put("b_0", namebludo);
	   						mbv2.put("b_1", pricebludo);
	   						mbv2.put("b_2", dostavkabludo);
	   						mbv2.put("b_3", weightpiudo);
	   						mbv2.put("b_4", picsmallpludo);
	   						mbv2.put("b_5", filepicsmallpludoPath);
	   						mbv2.put("b_6", parserxml2.nextText());
	   						bludodata2.add(mbv2);
	   					
	   				}


	   				parserxml2.next();

	   			}


	   			xmlfiles2.close();
	   			
	   			ItemLBludoAdapter2 = new ListBludo(getApplicationContext(), bludodata2, sizes);
	   			ItemLBludoAdapter2.notifyDataSetChanged();
	   						
	   			
	   		}
	   		catch (Throwable t) {

	   		}	
	   	}
	   	///\\\	
	   	public void openlisttab3(String bpath3,String bpathfull3) throws InterruptedException {
	   		bludodata3 =  new ArrayList<HashMap<String, String>>();
	   		try {

	   			XmlPullParserFactory parserxmlpull3 = XmlPullParserFactory.newInstance();
	   			parserxmlpull3.setValidating(false);
	   			XmlPullParser parserxml3 = parserxmlpull3.newPullParser();
	   			File xmlfileprices3 = new File(bpathfull3);
	   			InputStream xmlfiles3 = new FileInputStream(xmlfileprices3);
	   			parserxml3.setInput(xmlfiles3, null);
	   		
	   			while (parserxml3.getEventType()!= XmlPullParser.END_DOCUMENT) {

	   				if (parserxml3.getEventType() == XmlPullParser.START_TAG
	   						&& parserxml3.getName().equals("dish_drink") 
	   						&& parserxml3.getDepth() == 3) {
	   					//
	   					mbv3 = new HashMap<String, String>();
	   					String namebludo = parserxml3.getAttributeValue(0);
	   					if (namebludo == null) {
	   						namebludo = "";
	   					}else {
	   						namebludo = parserxml3.getAttributeValue(0);	
	   					}
	   					//
	   					String pricebludo = parserxml3.getAttributeValue(1);
	   					if (pricebludo == null) {
	   						pricebludo = "";
	   					}else {
	   						pricebludo = parserxml3.getAttributeValue(1);	
	   					}
	   					//
	   					String dostavkabludo = parserxml3.getAttributeValue(2);
	   					if (dostavkabludo == null) {
	   						dostavkabludo = "";
	   					}else {
	   						dostavkabludo = parserxml3.getAttributeValue(2);	
	   					}
	   					//
	   					String weightpiudo = parserxml3.getAttributeValue(3);
	   					if (weightpiudo == null) {
	   						weightpiudo = "";
	   					}else {
	   						weightpiudo = parserxml3.getAttributeValue(3);	
	   					}
	   					String picsmallpludo = parserxml3.getAttributeValue(4);
	   					if (picsmallpludo == null) {
	   						picsmallpludo = "";
	   					}else {
	   						picsmallpludo = parserxml3.getAttributeValue(4);	
	   					}
	   					
	   						String filepicsmallpludoPath = "";						
	   						filepicsmallpludoPath = bpath3+File.separator + picsmallpludo;
	   						
	   						mbv3.put("b_0", namebludo);
	   						mbv3.put("b_1", pricebludo);
	   						mbv3.put("b_2", dostavkabludo);
	   						mbv3.put("b_3", weightpiudo);
	   						mbv3.put("b_4", picsmallpludo);
	   						mbv3.put("b_5", filepicsmallpludoPath);
	   						mbv3.put("b_6", parserxml3.nextText());
	   						bludodata3.add(mbv3);
	   					
	   				}


	   				parserxml3.next();

	   			}


	   			xmlfiles3.close();
	   			
	   			ItemLBludoAdapter3 = new ListBludo(getApplicationContext(), bludodata3, sizes);
	   			ItemLBludoAdapter3.notifyDataSetChanged();
	   						
	   			
	   		}
	   		catch (Throwable t) {

	   		}	
	   	}
	   	///\\\	  
	       
	}
	
	public void trashlist() {
    	
     	String selblt = "SELECT " + Zakaz.DNAMEDISH + ", "
     			 + Zakaz.DPRICE + ", " + Zakaz.DDOSTAVKA + ", " + Zakaz.DVES + ", " + Zakaz.DPIC + ", " + Zakaz.DPICFULL + ", " + 
     			 Zakaz.DSOSTAV + 
     			 ", COUNT(*) " + 
     			 " FROM " + Zakaz.dtable+
     			 " GROUP BY "+ Zakaz.DNAMEDISH + ", "
     			 + Zakaz.DPRICE + ", " + Zakaz.DDOSTAVKA + ", " + Zakaz.DVES + ", " + Zakaz.DPIC + ", " + Zakaz.DPICFULL + ", " + 
     			 Zakaz.DSOSTAV + 
     			 " ";
     	//Zakaz sqldbctrash;
    	
     // �������������� ��� �����
    	Zakaz sqldbctrash = new Zakaz(getApplicationContext());
		// ���� ������ � ������
		SQLiteDatabase sqldbtrash = sqldbctrash.getWritableDatabase();
		
		Cursor  cursorbl0 = sqldbtrash.rawQuery(selblt, null);
		
		
		ctbl1 =  cursorbl0.getCount();
		bludodatatrash =  new ArrayList<HashMap<String, String>>();
		
		
		
		
		contpoz = 0;
		sumpoz = (double) 0;
		if (ctbl1 > 0) {
			
			while (cursorbl0.moveToNext()) {
				
				mbvtrash = new HashMap<String, String>();
				mbvtrash.put("tr_0", cursorbl0.getString(0));
				mbvtrash.put("tr_1", Double.valueOf(cursorbl0.getDouble(1)).toString());
				mbvtrash.put("tr_2", cursorbl0.getString(2));
				mbvtrash.put("tr_3", cursorbl0.getString(3));
				mbvtrash.put("tr_4", cursorbl0.getString(4));
				mbvtrash.put("tr_5", cursorbl0.getString(5));
				mbvtrash.put("tr_6", cursorbl0.getString(6));
				mbvtrash.put("tr_7", Integer.valueOf(cursorbl0.getInt(7)).toString());
				contpoz = contpoz + cursorbl0.getInt(7);
				sumpoz = sumpoz + (cursorbl0.getInt(7)*cursorbl0.getDouble(1));
				bludodatatrash.add(mbvtrash);
				
			}
			
		} 
		cursorbl0.close();
		sqldbtrash.close();
	    sqldbctrash.close(); 
	   
		ItemLBludoAdaptertrash = new ListBludoTrash(getApplicationContext(), bludodatatrash, sizes, getString(R.string.valut), getString(R.string.izm));
		ItemLBludoAdaptertrash.notifyDataSetChanged();
		if (ctbl1 > 0) {
			textView_itogo_.setText(getString(R.string.vsego)+" "+Integer.valueOf(contpoz).toString()+" "+getString(R.string.pozsum)+" "+Double.valueOf(sumpoz).toString()+" "+getString(R.string.valut));
			not_list_zakaz_.setVisibility(View.GONE);
			llistbludotrash.setVisibility(View.VISIBLE);
		} else {
				textView_itogo_.setText(R.string.tr_nol_t);
				llistbludotrash.setVisibility(View.GONE);
				not_list_zakaz_.setVisibility(View.VISIBLE);						
				}
    
     llistbludotrash.setAdapter(ItemLBludoAdaptertrash);
     ItemLBludoAdaptertrash.notifyDataSetChanged();
	}
	public void trashHandler() {
		Handler handlerListTrash = new Handler();
		handlerListTrash.post(new Runnable() {
			@Override
			public void run() {				
				trashlist();
			
			}
			
		});
	}
	//
	public void nomediapfos(String pricenomediafull) {

		File flnomedia = new File(pricenomediafull);
		flnomedia.getParentFile().mkdirs();
		if	(flnomedia.getParentFile().isDirectory()) {

		}
		FileOutputStream fosnomedia = null;
		try {
			fosnomedia = new FileOutputStream(flnomedia);

		} catch (FileNotFoundException e) {
			
		}

		try {
			fosnomedia.close();
		} catch (IOException e) {
			
		}
	}
	//
	protected void NotInet() {
		AlertDialog.Builder builder4 = new AlertDialog.Builder(this);
		builder4.setTitle(R.string.tit_connect);
		builder4.setMessage(R.string.mes_connect);
		builder4.setNeutralButton(R.string.botclose, null);
		builder4.create().show();	
	}
	
	//
	//listview open
	public void openlist() {
		tmp = "";
		tmp_stag = "";
		tmp_etag = "";
		tmp_text_tag = "";
		tmp_atag = "";
		try { 
			//

			//

			XmlPullParserFactory parserpull = XmlPullParserFactory.newInstance();
			parserpull.setValidating(false);
			parser = parserpull.newPullParser();
			File xmlfilesanatories = new File(spprisepathfull);
			xmlfile = new FileInputStream(xmlfilesanatories);
			parser.setInput(xmlfile, null);

			while (parser.getEventType()!= XmlPullParser.END_DOCUMENT) {

				// �������� xml ����� ������
				if (spchberror == true) {

					switch (parser.getEventType()) { 
					// ������ ��������� 
					case XmlPullParser.START_DOCUMENT: 		              
						break; 
						// ������ ���� 
					case XmlPullParser.START_TAG: 
						tmp_stag = "";		       		
						tmp_stag = "START_TAG: name  " + parser.getName() 
								+ ", depth  " + parser.getDepth() + ", attrCount  " 
								+ parser.getAttributeCount(); 
						tmp = ""; 
						for (int i = 0; i < parser.getAttributeCount(); i++) { 
							tmp = tmp + parser.getAttributeName(i) + "  " 
									+ parser.getAttributeValue(i) + ", "; 
						} 
						if (!TextUtils.isEmpty(tmp)) 
							tmp_atag = "Attributes: " + tmp; 
						break; 
						// ����� ���� 
					case XmlPullParser.END_TAG: 
						tmp_etag = "END_TAG: name  " + parser.getName(); 
						break; 
						// ���������� ���� 
					case XmlPullParser.TEXT:
						tmp_text_tag = "text  " + parser.getText(); 
						break; 
					default: 
						break; 
					}

				}

				// �������� xml ����� �����	

				parser.next();
			}
			xmlfile.close();
		}
		catch (Throwable terror) {
			if (spchberror == true) {
				Toast.makeText(getApplicationContext(),getString(R.string.error_loadxml)+": \n" + terror.toString(),Toast.LENGTH_LONG ).show();
			}

			File flprices = new File(spprisepathfull);
			flprices.getParentFile().mkdirs();
			if	((flprices.getParentFile().isDirectory()) & (!flprices.exists())) {
				spchbload = false;
				Qloadprice(0);	
			}			
		}
	}
	
	
	@Override 
	protected void onDestroy() {
		adView.destroy();
		System.gc();		 
		super.onDestroy();
		
	}
	 @Override 
	  protected void onStop() { 
	    super.onStop();
	    
	    sqldb.close();
	    sqldbc.close(); 
	  } 
	

	protected void onRestoreInstanceState(Bundle savedInstanceState) { 

		super.onRestoreInstanceState(savedInstanceState); 
		pdshow = savedInstanceState.getInt("pdshow");
		cract = savedInstanceState.getInt("cract"); 
		spchbload = savedInstanceState.getBoolean("spchbload");
		spchberror = savedInstanceState.getBoolean("spchberror");
		chbpach = savedInstanceState.getBoolean("chbpach");
		spprisepath = savedInstanceState.getString("spprisepath");
		spprisepathfull = savedInstanceState.getString("spprisepathfull");

		
	} 

	protected void onSaveInstanceState(Bundle outState) { 
		super.onSaveInstanceState(outState); 
		outState.putInt("pdshow", pdshow);
		outState.putInt("cract", cract);
		outState.putBoolean("spchbload",spchbload);
		outState.putBoolean("spchberror",spchberror);
		outState.putBoolean("chbpach",chbpach);
		outState.putString("spprisepath",spprisepath);
		outState.putString("spprisepathfull",spprisepathfull);

		
	} 
	protected void onResume() { 

		super.onResume();

		if (pdshow == 1) {
			ProgressD();
		}
		if (spchbload == true){
			
			if (cract == 0) {
				Startpserves(0);	            
			}
		}


	

	} 





	public boolean onCreateOptionsMenu(Menu menu) { 

		getMenuInflater().inflate(R.menu.pmenu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) { 
		menu.findItem(R.id.loaditem1).setEnabled(true);
		menu.findItem(R.id.pitem2).setEnabled(true);
		menu.findItem(R.id.abitem3).setEnabled(true);	
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) { 

		case R.id.loaditem1: 
			//load
			Qloadprice(1);
			break; 

		case R.id.pitem2:
			//���������
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferAmidoriya.class);
				startActivityForResult(Preferintent, 0);
			} catch (Exception e) {

			}
			break; 
		case R.id.abitem3: 
			//� ���������
			try {
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);
				startActivityForResult(intentapp, 0);
			} catch (Exception e) {

			}
			break;
		}       


		return super.onOptionsItemSelected(item); 

	} 


	public void Startpserves(int p_runs) {
		cract=1;
		try {
			intent = new Intent(getApplicationContext(), Amidoriyasermenu.class); 
			pi = createPendingResult(TASK_CODE, intent, 0);
			intent.putExtra(PARAM_RUNS, p_runs)
			.putExtra("spprisepath", spprisepath)
			.putExtra("spprisepathfull", spprisepathfull)
			.putExtra(PARAM_PINTENT, pi);
			// �������� ������
			startService(intent); 
			} catch (Exception e) {

		}

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == STATUS_START) { 
		} 

		if (resultCode ==  STATUS_FINISH) { 
			int result = data.getIntExtra(PARAM_RESULT, 0);	      	 


			if (result == 1) { 
				pd.hide();
				pdshow = 0;
				/*try {
					trashHandler();
					Handler handlerListAR = new Handler();
					handlerListAR.post(new Runnable() {
						@Override
						public void run() {				
							trashlist();
							ItemLBludoAdapter1.notifyDataSetChanged();
							ItemLBludoAdapter2.notifyDataSetChanged();
							ItemLBludoAdapter3.notifyDataSetChanged();
							
						}
						
					});
					
				} catch (Exception e) {

				}
				*/
				try {
					trashHandler();
					trashlist();
					ItemLBludoAdapter1.notifyDataSetChanged();
					ItemLBludoAdapter2.notifyDataSetChanged();
					ItemLBludoAdapter3.notifyDataSetChanged();
					
				} catch (Exception e) {

				}
				openlist();	
				Bundle savedInstanceState = null;
				super.onCreate(savedInstanceState);
				
				Toast.makeText(this,getString(R.string.okload),Toast.LENGTH_LONG ).show();
				restartAPP();
			} 
			if (result == 2) {
				pd.hide();
				pdshow = 0;			 
				NotInet();

				System.gc();
			} 

		}


		if (resultCode ==  STATUS_QUERY) {
			int result = data.getIntExtra(PARAM_RESULT, 0);
			
			Queryloadprice(); 


		}

		if (resultCode ==  7) { 

			ProgressD();
			pdshow = 1;
			Startpserves(1); 

		}

		if (resultCode ==  5) {
			try {
				openOptionsMenu();
			} catch (Exception e) {

			}
		}
		if (resultCode ==  9) {
			try {
				Intent Preferintent = new Intent(getApplicationContext(),PreferAmidoriya.class);

				startActivityForResult(Preferintent,0);
			} catch (Exception e) {

			}
		}
		if (resultCode ==  10) {	
			try {
				Intent intentapp = new Intent(getApplicationContext(), Aboutapp.class);

				startActivityForResult(intentapp,0);	
			} catch (Exception e) {

			}
		}
		if (resultCode ==  13) {
			try {
				trashHandler();
				Handler handlerListTrashAR = new Handler();
				handlerListTrashAR.post(new Runnable() {
					@Override
					public void run() {				
						trashlist();
						ItemLBludoAdapter1.notifyDataSetChanged();
						ItemLBludoAdapter2.notifyDataSetChanged();
						ItemLBludoAdapter3.notifyDataSetChanged();
					}
					
				});
				
			} catch (Exception e) {

			}
		}

	}
	protected void ProgressD() {

		pd.setCancelable(false);

		pd.setMessage(getString(R.string.pdm_app));	   
		pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pd.setButton(Dialog.BUTTON_NEGATIVE,getString(R.string.pdbutton_neg), new OnClickListener() { 

			public void onClick(DialogInterface dialog, int which) { 
				try {	            		 

					stopService(new Intent(getApplicationContext(), Amidoriyasermenu.class)); 

					pd.hide();
					pdshow = 0;	
					openlist();
				} catch (Exception e) {

				}

			} 

		}); 

		pd.show();

	} 
	protected void ProgressTask() {

		pdtask.setCancelable(false);
		pdtask.setMessage(getString(R.string.tdm_app));	   
		pdtask.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		pdtask.show();

	}
	protected void Queryloadprice() {
		AlertDialog.Builder builder2 = new AlertDialog.Builder(this);
		builder2.setTitle(R.string.lqt_app);
		builder2.setMessage(R.string.lqm_app);
		builder2.setNegativeButton(R.string.qn_app, null);
		builder2.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				ProgressD();
				pdshow = 1;
				Startpserves(1);

			}
		}).create().show();	
	}
	protected void Qloadprice(Integer rejim) {
		AlertDialog.Builder builder3 = new AlertDialog.Builder(this);
		builder3.setTitle(R.string.lt_app);
		if (rejim == 0) {
			builder3.setMessage(R.string.lmm_app);	
		}
		else {
			builder3.setMessage(R.string.lm_app);
		}
		builder3.setNegativeButton(R.string.qn_app, null);
		builder3.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				ProgressD();
				pdshow = 1;
				Startpserves(1);

			}
		}).create().show();	
	} 
	public void SAForR(Intent intent) {
	startActivityForResult(intent,0);
	}
	public void SAText() {
		Toast.makeText(getBaseContext(), getString(R.string.trashdel), Toast.LENGTH_SHORT).show(); 
		
		}
	//	    
	/////
	public void Menulogo() {
		dialogmenulogo = new AlertDialog.Builder(this);
		dialogmenulogo.setTitle("");		
		dialogmenulogo.setItems(spisokdmenulogo, 
				new DialogInterface.OnClickListener() {					
			@Override
			public void onClick(DialogInterface dialog, int item) {
				switch (item) { 

				case 0:
					try{
						openOptionsMenu();	

					} catch (Exception e) {

					}
					break;
					
				case 1:

					try {
						quitapp();
					} catch (Exception e) {

					}
					break; 

				}       					
			}
		}				
				).create().show();


	}
	
	public void quitapp() {

		AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
		builder1.setTitle(R.string.qt_app);
		builder1.setMessage(R.string.qm_app);
		builder1.setNegativeButton(R.string.qn_app, null);
		builder1.setNeutralButton(R.string.appocenkam, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				try {
					Intent ocenka = new Intent(Intent.ACTION_VIEW);
					ocenka.setData(Uri.parse(getString(R.string.appocenkauri)));
					startActivity(ocenka);
				} catch (Exception e) {
					Intent ocenkabr = new Intent(Intent.ACTION_VIEW,Uri.parse(getString(R.string.appocenkauribrouse)));
					startActivity(ocenkabr);
				}
			}
		});
		builder1.setPositiveButton(R.string.qy_app, new OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				MainActivity.super.onBackPressed();
				try {
					// ������������� ������ 
					stopService(new Intent(getApplicationContext(), Amidoriyasermenu.class)); 
					
				} catch (Exception e) {

				}
				MainActivity.super.onDestroy();
				System.gc();
			}
		});
		
		builder1.create().show();	
	}
	private void restartAPP() {
		Intent i = getBaseContext().getPackageManager()
	             .getLaunchIntentForPackage( getBaseContext().getPackageName() ); 
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
		//startActivity(i);
		startActivityForResult(i,0);
	}
	@Override
	public void onBackPressed() {
		quitapp();
	}
	/////

}
